package com.gxa.servlet;

import com.alibaba.fastjson.JSON;
import com.gxa.service.CommunityService;
import com.gxa.service.impl.CommunityServiceImpl;
import com.gxa.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/page/community/delete")
public class CommunityDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");

        PrintWriter out = resp.getWriter();
        Result result = new Result(0,"success",null);

        try {
            String cid = req.getParameter("cid");
            CommunityService communityService = new CommunityServiceImpl();
            communityService.delete(Integer.parseInt(cid));
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(1);
            result.setMsg("删除失败");
        }finally {
            String jsonStr = JSON.toJSONString(result);
            out.print(jsonStr);
        }



    }
}
